package com.heap.max;

import java.util.Random;

public class PerformanceTest {


    private static double testHeap(Integer[] testData, boolean isHeapify) {

        long startTime = System.nanoTime();
        MaxHeap<Integer> maxHeap;
        if (isHeapify) {
            maxHeap = new MaxHeap<>(testData);
        } else {
            maxHeap = new MaxHeap<>();
            for (int num: testData) {
                maxHeap.add(num);
            }
        }

        // 验证堆的正确性
        int n = testData.length;
        int[] arr = new int[n];
        for (int i = 0 ; i < n ; i ++ ) {
            arr[i] = maxHeap.extractMax();
        }
        for (int i = 1 ; i < n ; i ++ ) {
            if (arr[i - 1] < arr[i]) {
                throw new IllegalArgumentException("Error");
            }
        }
        System.out.println("Testing passed!");

        long endTime = System.nanoTime();
        return (endTime - startTime) / 1000000000.0;
    }

    public static void main(String[] args) {
        int n = 3000000;
        Random random = new Random();
        Integer[] testData = new Integer[n];
        for (int i = 0 ; i < n ; i ++) {
            testData[i] = random.nextInt(Integer.MAX_VALUE);
        }
        double time1 = testHeap(testData, false);
        System.out.println("without heapify: " + time1 + " s");

        double time2 = testHeap(testData, true);
        System.out.println("with heapify: " + time2 + " s");
    }
}
